#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "protocol.hpp"

#define NUM 1024

class CalClient
{
public:
    CalClient(const std::string &serverip, const uint16_t &serverport)
        : _sock(-1), _serverip(serverip), _serverport(serverport)
    {
    }
    ~CalClient()
    {
        if (_sock >= 0)
            close(_sock);
    }
    void initClient()
    {
        // 1. 创建socket
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if (_sock < 0)
        {
            std::cerr << "socket create error" << std::endl;
            exit(2);
        }
        // 2. tcp的客户端不用显式的bind，这里client port要让OS自己随机指定！
        // 不要listen和accept，只需要发起连接
    }
    void start()
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof server);
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(_serverip.c_str()); // 点分十进制转网络字节序
        server.sin_port = htons(_serverport);

        if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0)
        {
            std::cerr << "sock connect error" << std::endl;
        }
        else
        {
            std::string input;
            std::string inbuffer;
            while (1)
            {
                std::cout << "Cal: ";
                std::getline(std::cin, input);
                // 构建请求
                Request req = parse(input);
                // 序列化
                std::string req_str;
                req.serialize(&req_str);
                // 添加协议规则
                std::string send_str = enLength(req_str);
                // 发送
                std::cout << "send_str: " << send_str << std::endl;
                send(_sock, send_str.c_str(), send_str.size(), 0);

                std::string package; // 完整报文
                std::string text;    // 去报头后有效数据
                // 取出完整报文
                if (!recvPackage(_sock, inbuffer, &package))
                    continue;
                // 去报头拿到有效数据
                std::cout << "client 去报头..." << std::endl;

                if (!deLength(package, &text))
                    continue;
                std::cout << "client 反序列化..." << std::endl;
                // 得到响应
                Response resp;
                // 反序列化
                resp.deserialize(text);
                std::cout << "exitCode: " << resp._exitCode << std::endl;
                std::cout << "result: " << resp._result << std::endl;
            }
        }
    }
    Request parse(std::string &input)
    {
        // 1+1, 123*234
        int i = 0;
        while (isdigit(input[i]))
            ++i;
        std::string left = input.substr(0, i);
        char op = input[i];
        std::string right = input.substr(i + 1);
        int left_num = std::stoi(left);
        int right_num = std::stoi(right);
        std::cout << "计算式：" << left_num << op << right_num << std::endl;
        return Request(left_num, right_num, op);
    }

private:
    int _sock;
    std::string _serverip;
    uint16_t _serverport;
};